const app = getApp()
const api = require('../../utils/api.js');
const util = require('../../utils/util.js');

Page({
  data: {

    hasUserInfo: false,
    hasLogin: false,
    userInfo: {},
    memberInfo: null,
    mobile: null,

    cashTotal: 0,//零钱余额
    coinTotal: 0,//金币余额

    //二维码
    QRcode: {
      icon: '../../image/my/u1488.png',
      path: '/pages/address/list/index'
    },

    items: [
      {
        icon: '../../image/my/u1524.png',
        text: '收货地址',
        path: '/pages/my/address/list/list'
      },
      {
        icon: '../../image/my/u1526.png',
        text: '我的收藏',
        path: '/pages/my/collection/list',
      },
      {
        icon: '../../image/my/u1514.png',
        text: '关于我们',
        path: '/pages/my/aboutUs/aboutUs'
      },
      {
        icon: '../../image/my/u1528.png',
        text: '联系客服',
        path: '/pages/my/contaCt/contaCt'
      },
    ],


  },


  onLoad() {
    console.log("-----------my onLoad () start----------------")
    console.log("-----------我是分界线 全局变量 app.globalData----------------")
    console.log(app.globalData)






    try {
      var value = wx.getStorageSync('userInfo')
      if (value) {
        console.log("-----------本地缓存的数据 userInfo----------------")
        console.log(value)
        // Do something with return value
      }
    } catch (e) {
      // Do something when catch error
    }

/*     this.setData({
      userInfo: app.globalData.userInfo,
      memberInfo: app.globalData.memberInfo,
      hasLogin: app.globalData.hasLogin
    }) */



    //是否需要登陆
    if (app.globalData.hasLogin === false && app.globalData.userCancellLogin === false) {//需要登录
      wx.showModal({
        title: '温馨提示',
        content: '您还没有登录，是否要登录？',
        showCancel: true,
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login',
            })
          } else {
            app.globalData.userCancellLogin = true;
          }
        }
      })
    }



    console.log("-----------my onLoad () end----------------")

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log("-------- onReady()-------------")

    console.log("-----------我是分界线 this.data----------------")
    console.log(this.data)

    console.log("-----------我是分界线 this.data.userInfo----------------")
    console.log(this.data.userInfo)

    console.log("-------- onReady()-------------")
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {


    console.log("userInfo>>>>>>>>>>>>>>>>>>>")
    console.log(app.globalData.userInfo)
    if (app.globalData.userInfo !=null){
      console.log(app.globalData.userInfo.avatarUrl)
    }

    //获得零钱余额
    this.usableCashBalanceFind()

    //获得金币
    this.goldcoinFind()

    this.setData({
      userInfo: app.globalData.userInfo,
      memberInfo: app.globalData.memberInfo,
      hasLogin: app.globalData.hasLogin
    })

  },


  //获得零钱余额
  usableCashBalanceFind: function (e) {

    if (app.globalData.hasLogin === false && app.globalData.userCancellLogin === false) {//需要登录
      /*        wx.showModal({
              title: '温馨提示',
              content: '您还没有登录，是否要登录？',
              showCancel: true,
              success: function (res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/pages/login/login',
                  })
                } else {
                  app.globalData.userCancellLogin = true;
                }
              }
            })  */
    } else {

      let paramsData = {
        member_id: app.globalData.memberInfo.member_id,
        member_type_key: 'consumer',
      }
      //查询零钱余额接口
      const data = {
        service: 'finance.usableCashBalanceFind',
        params: JSON.stringify(paramsData),
        url_type: 'finance',

        app_token: app.globalData.app_token,
        security_code: app.globalData.security_code

      }

      var that = this
      api.requestData({
        data,
        success: (res) => {
          var resData = res.data;
          if (resData.rsp_code == 'succ') {
            that.setData({
              cashTotal: resData.data.usable_cash_balance,
            });

          }
        },
        fail: (res) => {
          console.log(res.data);
        }
      })
    }
  },



  //去二维码页面
  toQRcode(e) {

    var url = '/pages/my/qrcode/qrcode?cash=' + this.data.cashTotal
    this.NavigateUrl(url);



    //console.log(e)
    //var cash = e.currentTarget.dataset.cash
    //app.WxService.navigateTo('/pages/my/qrcode/qrcode')

    /*       console.log("---------------------------")
          console.log(cash)
          console.log(this.data.cashTotal)
          console.log("---------------------------") */

    /*       wx.navigateTo({
            url: `/pages/my/qrcode/qrcode?cash=` + this.data.cashTotal,
          }); */

  },



  //获得金币余额
  goldcoinFind: function (e) {
    if (app.globalData.hasLogin === false && app.globalData.userCancellLogin === false) {//需要登录
      /*         wx.showModal({
              title: '温馨提示',
              content: '您还没有登录，是否要登录？',
              showCancel: true,
              success: function (res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/pages/login/login',
                  })
                } else {
                  app.globalData.userCancellLogin = true;
                }
              }
            })   */
    } else {
      let paramsData = {
        member_id: app.globalData.memberInfo.member_id,
      }
      //金币余额接口
      const data = {
        service: 'finance.memberAssetFind',
        params: JSON.stringify(paramsData),
        url_type: 'finance',

        app_token: app.globalData.app_token,
        security_code: app.globalData.security_code

      }

      var that = this
      api.requestData({
        data,
        success: (res) => {
          var resData = res.data;
          console.log("------------------");
          console.log(resData);

          if (resData.rsp_code == 'succ') {
            this.setData({
              coinTotal: resData.data.gold,
            });
          }

        },
        fail: (res) => {
          console.log(res.data);
        }
      })
    }
  },



  //去零钱列表页面
  cashListPage: function (e) {
    var url = '/pages/my/cash/cash?cashTotal=' + this.data.cashTotal
    this.NavigateUrl(url);
  },

  //去金币列表页面
  goldcoinListPage: function (e) {
    var url = '/pages/my/goldcoin/goldcoin?coinTotal=' + this.data.coinTotal
    this.NavigateUrl(url);
  },


  //先判断是否登录
  NavigateUrl: function (url) {
    if (app.globalData.hasLogin === false) {//需要登录
      wx.showModal({
        title: '温馨提示',
        content: '您还没有登录，是否要登录？',
        showCancel: true,
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }
        }
      })
    } else {
      wx.navigateTo({
        url: url,
      });
    }
  },






/*   ///获取会员扩展信息接口
  memberExternalAccountListFind: function (e) {
    const data = {
      service: 'memberExternalAccount.app.memberExternalAccountListFind',
      params: '{"member_id":"11432431","member_type_key":"consumer"}',

      url_type: 'member',
      app_token: app.globalData.app_token,
      security_code: app.globalData.security_code

    }

    api.reqData({
      data,
      success: (res) => {
        console.log("获取会员扩展信息接口 返回 res.data>>>" + res.data);
        var resData = res.data;
        console.log(resData);
      },
      fail: (res) => {
        console.log("fail 返回 res.data>>>" + res.data);
      }
    })

  }, */



/*    //获取会员信息
  consumerFind: function (e) {
    const data = {
      service: 'member.consumerFind',
      //params: '{"member_id":"11432431"}',
      params: '{"member_id":"10494374"}',
      url_type: 'member',

      app_token: app.globalData.app_token,
      security_code: app.globalData.security_code

    }

    api.reqData({
      data,
      success: (res) => {
        console.log("success 返回 res.data>>>" + res.data);
        var resData = res.data;
        console.log(resData);

        this.setData({
          hasUserInfo: true,
          userInfo: res.data.data,
        });

        app.globalData.hasUserInfo = true;
        app.globalData.userInfo = res.data.data;
      },
      fail: (res) => {
        console.log("fail 返回 res.data>>>" + res.data);
      }
    })


  },  */




/*   userValidate: function (e) {
    var user_token = app.globalData.memberInfo.user_token;
    const data = {
      user_token: user_token,
      app_token: app.globalData.app_token,
      security_code: app.globalData.security_code
    }

    api.userValidate({
      data,
      success: (res) => {
        console.log(" userValidate  返回 res.data>>>" + res.data);
        var resData = res.data;
        console.log(resData);

        console.log(" userValidateuserValidateuserValidateuserValidateuserValidateuserValidateuserV");

        if(resData.rsp_code == 'succ'){
          console.log("OKOKOKOKOKOK");
        }else{
          console.log("NONONONONONONONONONONONONONONONONON");
        }
      },
      fail: (res) => {
        console.log("fail 返回 res.data>>>" + res.data);
      }
    })
  },  */


  
  //luyou
  getUserInfo: function () {
    var that = this
    if (app.globalData.hasLogin === false) {
      console.log("----------app.globalData.hasLogin === false--------");
      wx.login({
        success: function (res) {
          var code = res.code;
          console.log("----------code is --------");
          console.log(code);
          console.log("----------code is --------");
          _getUserInfo()
        }
      })

    } else {
      console.log("-----------app.globalData.hasLogin === true----------------")
      _getUserInfo()
    }

    function _getUserInfo() {
      wx.getUserInfo({
        success: function (res) {

          console.log("-----------_getUserInfo  _getUserInfo----------------")
          console.log(res.userInfo)

          that.setData({
            hasUserInfo: true,
            userInfo: res.userInfo
          })

          //放置全局变量
          app.globalData.hasUserInfo = true;
          app.globalData.userInfo = res.userInfo

        }
      })
    }
  },
  //luyou end


  myNavigate(e) {
    const index = e.currentTarget.dataset.index
    const path = e.currentTarget.dataset.path

    //switch(index) {
    //	case 2:
    //		App.WxService.makePhoneCall({
    //			phoneNumber: path
    //		})
    //		break
    //	default:
    //App.WxService.navigateTo(path)



    if (path == '/pages/my/aboutUs/aboutUs') {
      wx.navigateTo({
        url: path,
      });
    } else {
      this.NavigateUrl(path)

    }

    //}
  },



  /*   clearStorage(e) {
      const index = e.currentTarget.dataset.index
      const path = e.currentTarget.dataset.path
  
      switch (index) {
        case 0:
          app.WxService.showModal({
            title: '友情提示',
            content: '确定要清除缓存吗？',
          })
            .then(data => data.confirm == 1 && app.WxService.clearStorage())
          break
        default:
          app.WxService.navigateTo(path)
      }
    }, */

  logout() {
    app.WxService.showModal({
      title: '友情提示',
      content: '确定要退出登录吗？',
    })
      .then(data => data.confirm == 1 && this.signOut())
  },


  signOut() {
    console.log("-------signOut-----------")


    this.setData({
      //hasUserInfo: true,/*  */
      userInfo: app.globalData.userInfo,
      memberInfo: null,
      hasLogin: false,
      cashTotal: 0,
      coinTotal: 0,
    })

    app.userSignOut();



/*     try {
      wx.clearStorageSync()
      app.globalData.memberInfo = null
      app.globalData.userInfo = null
      app.globalData.hasLogin = false
      //app.globalData.hasUserInfo = false
      app.globalData.city = null
      app.globalData.CurrentDetailAddress = null

      this.setData({
        //hasUserInfo: true,
        userInfo: app.globalData.userInfo,
        memberInfo: app.globalData.memberInfo,
        hasLogin: app.globalData.hasLogin,
        cashTotal: 0,
        coinTotal: 0,
      })

      wx.switchTab({
        url: '/pages/index/index'
      })

    } catch (e) {
      // Do something when catch error
    } */





  }

})